package com.demo.spring;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class SpringAOPTest {

	public static void main(String[] args) {
		List list = new ArrayList();
		Advice advice = new LogAdvice();
		List proxy = (List) getProxy(list, advice);
		proxy.add("aaa");
	}
	//获取代理对象
	public static Object getProxy(final Object target,final Advice advice){
		Object obj = null;
		ClassLoader classLoader = target.getClass().getClassLoader();
		Class[] interfaces = target.getClass().getInterfaces();
		
		obj = Proxy.newProxyInstance(classLoader, interfaces,  
				new  InvocationHandler(){
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						advice.doBefore(target, method, args);
						Object rev = method.invoke(target, args);
						advice.doAfter(target, method, args, rev);
						return rev;
					}
				});
		return obj;
	}
	
	
}
